<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="城市名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入城市名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:city:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:city:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cms:city:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['cms:city:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="城市名称" align="center" prop="name" />
      <el-table-column label="城市英文名称" align="center" prop="nameEn" />
      <el-table-column label="展示屏数量" align="center" prop="displayCount" />
       <el-table-column label="背景图" align="center" >
            <el-image slot-scope="scope" :src="scope.row.bgimage"  ></el-image>
      </el-table-column>
      <el-table-column label="跳转URL" align="center">
        <el-link slot-scope="scope" :href="scope.row.url" target="_blank">{{scope.row.url}}</el-link>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleDisplay(scope.row)"
            v-hasPermi="['cms:city:displays']"
          >关联展示屏</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:city:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:city:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改城市管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="城市名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入城市名称" />
        </el-form-item>
        <el-form-item label="城市英文名称" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入城市英文名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      <el-form-item label="选择背景图" prop="bgType">
          <el-radio-group v-model="form.bgType" size="medium">
            <el-radio v-for="(item, index) in bgTypeOptions" :key="index" :label="item.value"
              :disabled="item.disabled">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="背景图" prop="sort" >
            <el-image  :src="bg1"  title="背景一" style="width: 100px; height: 100px; margin-right: 10px;"></el-image>
            <el-image  :src="bg2"  title="背景二"  style="width: 100px; height: 100px;"></el-image>
        </el-form-item>
        <el-form-item label="跳转URL" prop="url">
          <el-input v-model="form.url" placeholder="请输入跳转URL" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 关联展示屏 -->
    <el-dialog v-loading="loading" :title="cityTitle" :visible.sync="displayOpen" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCDisplay(cityId)" v-hasPermi="['cms:city:displays']">添加关联</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDisplay" v-hasPermi="['cms:city:displays:delete']">取消关联</el-button>
        </el-col>
      </el-row>

      <el-table :data="displayList" @selection-change="handleCityDisplaySelectionChange" ref="cDisplay">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="展示屏ID" align="center" prop="id" width="100"/>
          <el-table-column label="展示屏名称" align="center" prop="name" />
          <el-table-column label="排序" align="center" prop="sort" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdateDisplay(scope.row)"
                v-hasPermi="['cms:city:displays:edit']"
              >修改</el-button>
            
            </template>
          </el-table-column>
      </el-table>  
    </el-dialog>

    <!-- 添加关联 -->
    <el-dialog v-loading="loading" :title="dislpayListTitle" :visible.sync="displayListOpen" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCityDisplay" v-hasPermi="['cms:city:displays:add']">确定</el-button>
        </el-col>
      </el-row>

      <el-table :data="displays" @selection-change="handleDisplaySelectionChange" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="展示屏ID" align="center" prop="id" width="100"/>
          <el-table-column label="展示屏名称" align="center" prop="name" />
          <el-table-column label="排序" align="center" prop="sort" />
      </el-table>  
    </el-dialog>

    <!-- 修改关联 -->
    <el-dialog :title="dislpayListTitle" :visible.sync="editDisplayOpen" width="600px" append-to-body>
      <el-form ref="form" :model="cityDisplayForm" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model="cityDisplayForm.sort" placeholder="请输入排序" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDisplayForm">确 定</el-button>
        <el-button @click="cancelDisplayForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCity, getCity, delCity, addCity, updateCity, exportCity, listDisplay, getAllDisplays, addCityDisplays, editCityDisplays, deleteCityDisplays } from "@/api/cms/city";
import bg1 from '@/assets/images/bg1.png';
import bg2 from '@/assets/images/bg2.png';

export default {
  name: "City",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCDisplay: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 城市管理表格数据
      cityList: [],
      // 展示屏管理表格数据
      displayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "城市名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
         bgType: [
           {
          required: true,
          message: '单选框组不能为空',
          trigger: 'change'
        }],
      },
      bgTypeOptions: [
        {
        "label": "背景一",
        "value": 1
      }, {
        "label": "背景二",
        "value": 2
      }],
      // 展示屏
      cityTitle: null,
      displayOpen: false,
      cityId: null,
      dislpayListTitle: "展示屏列表",
      displayListOpen: false,
      displays: [],
      cityDisplays: [],
      cityDisplayForm: {},
      editDisplayOpen: false,
      displayIds: [],
      //背景图路劲
      bg1: bg1,
      bg2: bg2,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //取消关联
    handleDeleteDisplay() {
      deleteCityDisplays(this.cityId, this.displayIds).then(response => {
        this.msgSuccess("取消成功");
        this.getDisplayList();
        this.getList();
      })
    },
    /** 多选选中展示屏 */
    handleCityDisplaySelectionChange(selection) {
      this.displayIds =  selection.map(item => item.id);
    },
    //提交关联修改
    submitDisplayForm() {
      editCityDisplays(this.cityDisplayForm).then(response => {
        this.msgSuccess("修改成功");
        this.editDisplayOpen = false;
        this.getDisplayList();
      })
    },
    //取消关联修改
    cancelDisplayForm() {
      this.editDisplayOpen = false;
    },
    //修改关联
    handleUpdateDisplay(row) {
      this.cityDisplayForm = {
        cityId: this.cityId,
        displayId: row.id,
        sort: row.sort
      }
      this.editDisplayOpen = true;
    },
    //添加关联
    handleAddCityDisplay() {
      
      addCityDisplays(this.cityDisplays).then(response => {
        this.msgSuccess("添加成功");
        this.displayListOpen = false;
        this.getDisplayList();
        this.getList();
      })
    },
    //多选选中展示屏
    handleDisplaySelectionChange(selection) {
      this.cityDisplays = [];
      selection.map(item => {
        item.cityId = this.cityId;
        item.displayId = item.id;
      })
      this.cityDisplays = selection;
    },
    /** 展示屏管理添加按钮操作 */
    handleAddCDisplay(id) {
      this.displayListOpen = true;
      this.loading = true;
      getAllDisplays(id).then(response => {
        this.displays = response.data;
        this.loading = false;
      })
    },
    // 关联展示屏
    handleDisplay(row) {
      this.cityTitle = row.name + "—展示屏";
      this.displayOpen = true;
      this.cityId = row.id;
      this.getDisplayList()
    },
    //展示屏数据
    getDisplayList() {
      this.loading = true;
      listDisplay(this.cityId).then(response => {
        this.displayList = response.data;
        this.loading = false;
      })
    },
    /** 查询城市管理列表 */
    getList() {
      this.loading = true;
      listCity(this.queryParams).then(response => {
        this.cityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        nameEn: null,
        displayCount: null,
        bgimage: null,
        bgType: 1,
        url: null,
        sort: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加城市管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改城市管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCity(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCity(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除城市管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有城市管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCity(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
